package com.woniuxy.exam.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.commons.dto.QuestionDto;
import com.woniuxy.commons.entity.BlankFilling;
import com.woniuxy.commons.entity.ResponseEntity;
import com.woniuxy.commons.utils.TimeUtils;
import com.woniuxy.exam.service.BlankFillingService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zouyang
 * @date 2020/7/17 - 23:30
 */
@RestController
public class BlankingFillingController {

    @Autowired
    private BlankFillingService blankFillingService;

    @RequestMapping("/bf/all")
    public List<BlankFilling> getAll(){
        return blankFillingService.selectAll();
    }

    @RequestMapping("/bf/add")
    public ResponseEntity add(@RequestBody BlankFilling blankFilling){
        blankFilling.setCreateDate(TimeUtils.getNowTime());
        int res = blankFillingService.insert(blankFilling);
        if(res!=1){
            return new ResponseEntity(500,"系统繁忙");
        }
        return ResponseEntity.SUCCESS_RESPONSE;
    }

    @GetMapping("/bf/find/{id}")
    public BlankFilling find(@PathVariable("id") int id){
        return blankFillingService.selectByPrimaryKey(id);
    }

    @RequestMapping("/bf/search")
    public List<BlankFilling> search(QuestionDto questionDto){
        BlankFilling blankFilling = new BlankFilling();
        BeanUtils.copyProperties(questionDto,blankFilling);
        return blankFillingService.selectByAll(blankFilling);
    }

    @PostMapping("/bf/caps")
    public ResponseEntity conditionAndPageSearch(QuestionDto questionDto){
        BlankFilling blankFilling = new BlankFilling();
        BeanUtils.copyProperties(questionDto,blankFilling);
        PageHelper.startPage(questionDto.getPageNum(),questionDto.getPageSize());
        List<BlankFilling> list =  blankFillingService.selectByAll(blankFilling);
        PageInfo pageInfo = new PageInfo(list,10);
        return new ResponseEntity(200,"success",pageInfo);
    }

    @RequestMapping("/bf/update")
    public ResponseEntity update(BlankFilling blankFilling){
        int res = blankFillingService.updateByPrimaryKeySelective(blankFilling);
        if(res!=1){
            return new ResponseEntity(500,"出现异常");
        }
        return new ResponseEntity(200,"success");
    }

    @RequestMapping("/bf/delete")
    public ResponseEntity delete(int id){
        int res = blankFillingService.deleteByPrimaryKey(id);
        if(res!=1){
            return new ResponseEntity(500,"出现异常");
        }
        return new ResponseEntity(200,"success");
    }

}